<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="800"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px">
      <el-tabs type="border-card">
        <el-tab-pane ref="tab" label="基础信息">
          <el-row justify="space-evenly">
            <el-col :span="24">
              <el-descriptions title="" :column="2" border label-align="right">
                <el-descriptions-item>
                  <template #label>单据日期</template>
                  {{ form.created_at }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>单号</template>
                  {{ form.main.code }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>客户品名</template>
                  {{ form.custno }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>颜色</template>
                  {{ form.color_name }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>缸号</template>
                  {{ form.dyelot }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>膜/布</template>
                  <el-input v-model="search.material"></el-input>
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>开卡匹</template>
                  {{ form.auxiliary_quantity }}
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>棍</template>
                  <el-input v-model="search.pointStick"></el-input>
                </el-descriptions-item>

                <el-descriptions-item>
                  <template #label>开卡数</template>
                  {{ form.quantity }}
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>胶水</template>
                  <el-input v-model="search.glue"></el-input>
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>老工艺</template>
                  <el-input v-model="postdata.history_craft" readonly> </el-input>
                </el-descriptions-item>
                <el-descriptions-item>
                  <template #label>新工艺</template>
                  <skAutoChoose
                    style="width: 100%"
                    ref="autoBom"
                    :asKey="'bom_id'"
                    :assign="postdata"
                    v-model="postdata.new_craft"
                    :name="'bom'"
                    :placeholder="'请选择加工物料'"
                    :params="getProcessBomCategory"
                  >
                  </skAutoChoose>
                </el-descriptions-item>
              </el-descriptions>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()" v-if="!showonly"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script>
import tool from "@/utils/tool";
import skAutoChoose from "@/components/skAutoChoose";
export default {
  emits: ["success", "closed"],
  components: { skAutoChoose },
  data() {
    return {
      mode: "add",
      form: {
        material: {},
        main: {},
      },
      postdata: {},
      search: {},
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      rules: {},
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  watch: {
    search: {
      handler() {
        this.$refs.autoBom.triggerSelect();
      },
      deep: true,
    },
  },
  methods: {
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.pro.OpenCard.setCraft.post(this.postdata);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", res);
            this.$message.success("操作成功");
          }
        }
      });
    },
    getProcessBomCategory() {
      let params = { craft_category_id: this.form.craft_category_id };
      if (this.search.pointStick && this.search.pointStick.trim() != "") {
        params["details.point_stick.name"] = {
          value: this.search.pointStick,
          type: "lk",
        };
      }
      if (this.form.backing_category_id) {
        params["details.material.category_id"] = this.form.backing_category_id;
      }
      if (this.form.film_category_id) {
        params["details.material.film_category_id"] = this.form.film_category_id;
      }
      if (this.form.material_comp && this.form.material_comp.trim() != "") {
        params["details.material.comp"] = this.form.material_comp;
      }
      if (this.form.material_comp && this.form.material_comp.trim() != "") {
        params["details.material.comp"] = this.form.material_comp;
      }
      if (this.form.material_batch && this.form.material_batch.trim() != "") {
        params["details.material.batch"] = this.form.material_batch;
      }

      if (this.search.material && this.search.material.trim() != "") {
        params["details.material.fullname"] = {
          value: this.search.material.trim(),
          type: "lk",
        };
      }

      if (this.search.glue && this.search.glue.trim() != "") {
        params["details.glue.fullname"] = { value: this.search.glue.trim(), type: "lk" };
      }
      params["status_id"] = { value: 0, type: "eq" };
      return params;
    },
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //表单注入数据
    setData(data) {
      this.postdata.id = data.id;
      this.postdata.history_craft = data.main.bom.name;
      Object.assign(this.form, tool.removeNull(data));
      return this;
    },
  },
};
</script>
